package com.MarkToTop.main;

import com.MarkToTop.util.Constant;
import com.MarkToTop.util.GameUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 * MarkWang 版权所有 © Copyright 2020
 *
 * @CreateDate: Created in 2020-06-22 08:33
 * @Author: MarkToTop -(づ｡◕‿‿◕｡)づ
 * @Description:  游戏的前景 游戏上方遮挡层
 *                 存在多片云彩
 */
public class GameFrontGround {

    //用来管理多片云彩的容器
    private List<Cloud>  clouds;

    //使用的图片资源组
    private BufferedImage[] cloudImgs;

    //云彩的飞行状态
    private int CloudDir;
    //用于控制云的逻辑运算周期时间
    private long time;
    //逻辑运算时间间隔
    public static final int LOGIC_INITERVAL = 100;

    public GameFrontGround() {
        cloudImgs = new BufferedImage[2];
        clouds = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            cloudImgs[i] = GameUtil.loadBufferedImage(Constant.Clouds_PATH[i]);
        }
        time = System.currentTimeMillis();
        //速度为1，往左飘
        CloudDir = Cloud.DIR_LEFT;
    }

    /**
     * 前景的绘制
     * @param g
     */
    public void draw(Graphics g){

        Logic();

        for (int i = 0; i < clouds.size(); i++) {
            clouds.get(i).draw(g);
        }
    }

    /**
     * 关于容器中云彩的个数的控制,方向、速度、等逻辑
     */
    private void Logic(){
        //云的控制，每隔100ms运算一次
        if(System.currentTimeMillis() - time > LOGIC_INITERVAL){
            //更新时间戳
            time = System.currentTimeMillis();
            //以百分之二的概率加云彩
            if(clouds.size() < Constant.MAX_CLOUD_COUNT){
                if(GameUtil.isInPercent(Constant.CLOUD_BORN_PERCENT)){
                    int index = GameUtil.getRandomNumber(0, 2); //云图1、云图2的选择
//                    int x = -cloudImgs[index].getWidth(null);
                    //向左飘，起始位置在右边缘
                    int x = Constant.Frame_Width;
                    int y = GameUtil.getRandomNumber(Constant.TOP_BAR_HEIGHT, Constant.Frame_Height/3);
                    Cloud cloud = new Cloud(cloudImgs[index],CloudDir,x,y);
                    clouds.add(cloud);
                }
            }

            //云彩飞出屏幕后的操作
            for (int i = 0; i < clouds.size(); i++) {
                //遍历获得容器中每一朵云彩
                Cloud cloud = clouds.get(i);
                //判断是否飞出
                if (cloud.isOutFrame()){
                    //移除
                    clouds.remove(i);
                    i--;
                }
            }
            //System.out.println("当前屏幕中云朵个数："+clouds.size());

           /* try {
                //间隔10秒可能变化一次云彩的方向，模拟风
                if(GameUtil.isInAnyProbability(1,100)){
                    int newDir = GameUtil.getRandomNumber(Cloud.DIR_LEFT,Cloud.DIR_RIGHT+1);
                    System.out.println(newDir);
                    if (newDir != CloudDir){
                        System.out.println(newDir+"-----"+CloudDir);
                        CloudDir = newDir;
                        //云彩方向发生变化，重新设置方向
                        for (int i = 0; i < clouds.size(); i++) {
                            //遍历获得每朵云彩
                            Cloud cloud = clouds.get(i);
                            cloud.setDir(newDir);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            */
        }
    }
}
